//
//  BaseImageView.m
//  GK
//
//  Created by Seven on 2018/12/19.
//  Copyright © 2018 Seven. All rights reserved.
//

#import "BaseImageView.h"
#import "ITImageManager.h"
@implementation BaseImageView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (void)setFrame:(CGRect)frame{
    [super setFrame:frame];
}
- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.runloopMode = NSDefaultRunLoopMode;
        self.clipsToBounds = YES;
        self.contentMode = UIViewContentModeScaleAspectFill;
    }
    return self;
}

- (void)setImageWithUrl:(NSString *)url{
    [self setImageWithUrl:url placeholderName:nil];
}

- (void)setImageWithUrl:(NSString *)url placeholderName:(NSString *)name{
    UIImage *place = nil;
    if (name != nil) {
        if ([name isKindOfClass:UIImage.class]) {
            place = name;
        }else if ([name isKindOfClass:NSString.class] && name.length > 0) {
            place = [UIImage imageNamed:name];
        }
    }
    //https://oss.cdn.invid.me/admin/userImage/FDC9E656B17043B9A4B4744F5B86C9CC_1586838752917.jpg
    //https://x-intube-pro.oss-us-west-1.aliyuncs.com/admin/userImage/FDC9E656B17043B9A4B4744F5B86C9CC_1586838752917.jpg
    //url = [url stringByReplacingOccurrencesOfString:@"x-intube-pro.oss-us-west-1.aliyuncs.com" withString:@"oss.cdn.invid.me"];
    [self setImageWithURL:[NSURL URLWithString:url] placeholder:place options:YYWebImageOptionSetImageWithFadeAnimation completion:nil];
    
}

- (void)setImageWithName:(NSString *)name{
    [self setImage:[UIImage imageNamed:name]];
}



- (void)setUserIconWithUrl2:(NSString *)url{
    ITImageManager *_manager = [ITImageManager managerWithWidth:self.width];
    [self setImageWithURL:[NSURL URLWithString:url] placeholder:[UIImage imageNamed:@"UserHeadDefaultImage"] options:YYWebImageOptionSetImageWithFadeAnimation manager:_manager progress:nil transform:nil completion:nil];
}

- (void)setUserIconWithUrl:(NSString *)url{
    static NSString *_IMAGE_PORTRAIT_ZOOM_PARAM = nil;
    if (_IMAGE_PORTRAIT_ZOOM_PARAM == nil) {
        NSString *tmp = [ITGeneralManager.defaultManager configDicWithKey:@"DICT_IMAGE_PORTRAIT_ZOOM_PARAM"].value;
        _IMAGE_PORTRAIT_ZOOM_PARAM = tmp;
    }
    if (_IMAGE_PORTRAIT_ZOOM_PARAM.length > 0 && [url rangeOfString:@"?"].location == NSNotFound) {
        url = [url stringByAppendingFormat:@"%@",_IMAGE_PORTRAIT_ZOOM_PARAM];
    }
    [self setImageWithUrl:url placeholderName:@"UserHeadDefaultImage"];
    //[self setImageWithURL:[NSURL URLWithString:url] placeholder:[UIImage imageNamed:@"UserHeadDefaultImage"] options:YYWebImageOptionSetImageWithFadeAnimation manager:nil progress:nil transform:nil completion:nil];
}

- (void)setCustomAction:(CustomAction)customAction{
    _customAction = customAction;
    self.userInteractionEnabled = YES;
}

- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    if (self.customAction) {
        self.customAction(self, cat_tap);
    }
}


+ (void)saveUserImage:(UIImage *)image withKey:(NSString *)key{
    /*
    GlobalDelay(0, ^{
        CGFloat minWidth = image.size.width;
        CGFloat border = minWidth/100;
        UIImage * icon = [image imageByRoundCornerRadius:minWidth/2 borderWidth:border borderColor:ColorWithHex(0xBEBEBE)];
        //[_userIconManager.cache setImage:icon forKey:key];
    });
    //*/
    [self saveImage:image withKey:key];
}

+ (void)saveImage:(UIImage *)image withKey:(NSString *)key{
    GlobalDelay(0, ^{
        [YYImageCache.sharedCache setImage:image forKey:key];
    });
}



+ (void)imageWithUrl:(NSString *)url callback:(void (^)(UIImage *image))callback{
    if (url.length == 0) {
        if (callback) callback(nil);
        return;
    }
    UIImage *img = [YYImageCache.sharedCache getImageForKey:url];
    if (img != nil) {
        if (callback) callback(img);
    }
    __block BaseImageView *tmp = BaseImageView.new;
    [tmp setImageWithURL:[NSURL URLWithString:url] placeholder:nil options:nil completion:^(UIImage * _Nullable image, NSURL * _Nonnull url, YYWebImageFromType from, YYWebImageStage stage, NSError * _Nullable error) {
        if (callback) callback(image);
    }];
}

- (void)setHidden:(BOOL)hidden{
    [super setHidden:hidden];
}

- (void)setAlpha:(CGFloat)alpha{
    [super setAlpha:alpha];
}

+ (void)cacheUrl:(NSString *)url{
    
    __block BaseImageView *view = [BaseImageView new];
    [view setImageWithURL:[NSURL URLWithString:url] placeholder:nil options:nil completion:^(UIImage * _Nullable image, NSURL * _Nonnull url, YYWebImageFromType from, YYWebImageStage stage, NSError * _Nullable error) {
        view = nil;
    }];
}

@end
